package com.sensePlusVR.utils;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.main.R;
import com.sensePlusVR.vlc.misc.MediaDatabase;
import com.sensePlusVR.vlc.misc.MediaWrapper;
/**
 * 
 * @author: Loren
 * @create: 2016-6-14下午2:45:59
 * @desc: 图片处理
 *
 */
public class BitmapUtil extends AsyncTask<String, Void, Bitmap>{
	@SuppressWarnings("unused")
	private static final String TAG = BitmapUtil.class.getSimpleName();
	public interface Callback{
		
		public void onBitmap(Bitmap bitmap);
	}

	
	private Callback callback;
	
	
	public BitmapUtil(Callback callback){
		

		this.callback = callback;
	}
	

	@Override
	protected Bitmap doInBackground(String... params) {
		
		return BitmapFactory.decodeFile(params[0]);
		
	}
	
	@Override
	protected void onPostExecute(Bitmap result) {
		super.onPostExecute(result);
		
		
		if(callback!=null){
			
			callback.onBitmap(result);
		}
	}
	
	
	 public static Bitmap cropBorders(Bitmap bitmap, int width, int height){
	        int top = 0;
	        for (int i = 0; i < height / 2; i++) {
	            int pixel1 = bitmap.getPixel(width / 2, i);
	            int pixel2 = bitmap.getPixel(width / 2, height - i - 1);
	            if ((pixel1 == 0 || pixel1 == -16777216) &&
	                (pixel2 == 0 || pixel2 == -16777216)) {
	                top = i;
	            } else {
	                break;
	            }
	        }

	        int left = 0;
	        for (int i = 0; i < width / 2; i++) {
	            int pixel1 = bitmap.getPixel(i, height / 2);
	            int pixel2 = bitmap.getPixel(width - i - 1, height / 2);
	            if ((pixel1 == 0 || pixel1 == -16777216) &&
	                (pixel2 == 0 || pixel2 == -16777216)) {
	                left = i;
	            } else {
	                break;
	            }
	        }

	        if (left >= width / 2 - 10 || top >= height / 2 - 10)
	            return bitmap;

	        // Cut off the transparency on the borders
	        return Bitmap.createBitmap(bitmap, left, top,
	                (width - (2 * left)), (height - (2 * top)));
	    }

	    public static Bitmap scaleDownBitmap(Context context, Bitmap bitmap, int width) {
	        /*
	         * This method can lead to OutOfMemoryError!
	         * If the source size is more than twice the target size use
	         * the optimized version available in AudioUtil::readCoverBitmap
	         */
	        if (bitmap != null) {
	            final float densityMultiplier = context.getResources().getDisplayMetrics().density;
	            int w = (int) (width * densityMultiplier);
	            int h = (int) (w * bitmap.getHeight() / ((double) bitmap.getWidth()));
	            bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
	        }
	        return bitmap;
	    }

	    public static Bitmap getPictureFromCache(MediaWrapper media) {
	        // mPicture is not null only if passed through
	        // the ctor which is deprecated by now.
	        Bitmap b = media.getPicture();
	        if(b == null) {
	            BitmapCache cache = BitmapCache.getInstance();
	            Bitmap picture = cache.getBitmapFromMemCache(media.getLocation());
	            if(picture == null) {
	                /* Not in memcache:
	                 * serving the file from the database and
	                 * adding it to the memcache for later use.
	                 */
	                picture = readCoverBitmap(media.getArtworkURL());
	                if (picture == null)
	                    picture = MediaDatabase.getInstance().getPicture(media.getUri());
	                cache.addBitmapToMemCache(media.getLocation(), picture);
	            }
	            return picture;
	        } else {
	            return b;
	        }
	    }

	    private static Bitmap readCoverBitmap(String path) {
	        if (path == null)
	            return null;
	        Resources res = VRApplication.getAppResources();
	        String uri = Uri.decode(path);
	        if (uri.startsWith("file://"))
	            uri = uri.substring(7);
	        Bitmap cover = null;
	        BitmapFactory.Options options = new BitmapFactory.Options();
	        int height = res.getDimensionPixelSize(R.dimen.grid_card_thumb_height);
	        int width = res.getDimensionPixelSize(R.dimen.grid_card_thumb_width);

	        /* Get the resolution of the bitmap without allocating the memory */
	        options.inJustDecodeBounds = true;
	        BitmapFactory.decodeFile(uri, options);

	        if (options.outWidth > 0 && options.outHeight > 0) {
	            if (options.outWidth > width){
	                options.outWidth = width;
	                options.outHeight = height;
	            }
	            options.inJustDecodeBounds = false;

	            // Decode the file (with memory allocation this time)
	            try {
	                cover = BitmapFactory.decodeFile(uri, options);
	            } catch (OutOfMemoryError e) {
	                cover = null;
	            }
	        }

	        return cover;
	    }
	    
	    //private static ACache mACache;

		public static Bitmap getBitmapFromResID(Context context, int resID){
			/*mACache = ACache.get(VRApplication.getAppContext());
			String cacheKey = String.valueOf(resID);
			Bitmap bitmap = mACache.getAsBitmap(cacheKey);
			if (bitmap != null) {
				MyLog.d(TAG, "get bitmap from memory");
				return bitmap;
			}*/
			InputStream is = context.getResources().openRawResource(resID);
			BitmapFactory.Options opts = new BitmapFactory.Options();
			opts.inSampleSize = 1;
			opts.inPreferredConfig = Bitmap.Config.ARGB_8888;

			Bitmap bitmapTmp;
			try {
				bitmapTmp = BitmapFactory.decodeStream(is, null, opts);
				//mACache.put(cacheKey, bitmapTmp);
			} finally {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			return bitmapTmp;
		}
	
	
	

}
